﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVVMFramework.ViewModel;
using SharpSvn;

namespace SvnMonitor
{
    class NotificationVM : ViewModelBase
    {
        private readonly IRepositoryMonitor repositoryMonitor;

        public NotificationVM(SvnInfoEventArgs info, IRepositoryMonitor repositoryMonitor)
        {
            Info = info;
            this.repositoryMonitor = repositoryMonitor;
        }

        public string ProjectName
        {
            get
            {
                return repositoryMonitor.ProjectName;
            }
        }

        public string CommitInfo
        {
            get
            {
                StringBuilder sb = new StringBuilder(Info.LastChangeAuthor);
                sb.Append(", ");

                var changed = Info.LastChangeTime.ToLocalTime();
                var now = DateTime.Now;
                var diff = now - changed;
                if (diff.TotalMinutes < 1)
                    sb.AppendFormat("{0} sec. ago", (int)diff.TotalSeconds);
                else if (diff.TotalMinutes < 2)
                    sb.Append("a minute ago");
                else if (diff.TotalMinutes < 3)
                    sb.AppendFormat("{0} minutes ago", (int)diff.TotalMinutes);
                else if (now.Date == changed.Date)
                    sb.AppendFormat("{0}", changed.ToShortTimeString());
                else
                    sb.AppendFormat("{0}", changed);

                return sb.ToString();
            }
        }

        public SvnInfoEventArgs Info
        {
            get;
            private set;
        }

        public void UpdateToLatestVersion()
        {
            TortoiseSVN.UpdateWorkingCopy(repositoryMonitor);
            /* 
            using (SvnClient client = new SvnClient())
            {
               // Update the specified working copy path to the head revision
                SvnUpdateResult result;
                client.Update(workingCopy, out result);
            }
            */
        }
    }
}
